<?php
/**
 * 更新region表的location字段-修改市
 */

namespace app\crontab\controller\once;


use custom\Log;
use map\BaiDu;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Location extends Command {

    protected function configure() {
        $this->setName('location');
    }

    protected function execute(Input $input, Output $output) {
        $this->initial();
    }

    public function initial() {
        $i = 0;
        echo 'start';
        echo "\n";
        $data = Db::query('select parent.name as p_name,child.name as c_name,child.id from erp_region as parent join erp_region as child on parent.id=child.pid where child.rank=2');
        foreach ($data as $k => $v) {
            $res1 = BaiDu::getByAddress("{$v['p_name']}{$v['c_name']}", $v['p_name']);
            if (!empty($res1['result'])) {
                $lat = $res1['result']['location']['lat'];
                $lng = $res1['result']['location']['lng'];
                $res2 = Db::execute("update erp_region set location = POINT('{$lng}', '{$lat}') where id = '{$v['id']}'");
                if ($res2) {
                    $i++;
                }
            }
        }
        echo 'end';
        echo "\n";
        Log::write($i, 'locationErr.log');
        echo 'total:' . count($data) . ',success:' . $i;
    }

}
